package com.egame.ewidget.service;

import android.app.Service;
import android.content.ContentResolver;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;

import com.egame.ewidget.observer.BrightnessContentObserver;
import com.egame.ewidget.observer.BrightnessContentObserver.BrightnessStateChangedListener;
import com.egame.ewidget.utility.BrightnessUtility;
import com.egame.ewidget.utility.FlashTorchUtility;
import com.egame.ewidget.utility.MobileDataUtility;
import com.egame.ewidget.utility.RingerModeUtitliy;
import com.egame.ewidget.utility.WiFiUtility;
import com.egame.ewidget.utils.Utils;

public class WidgetUiService extends Service implements
		BrightnessStateChangedListener {
	public static final String ACTION_ENABLE = "egame.launcher.widgetutil.appwidgetutils.service.action.ACTION_ENABLE";

	public static final String ACTION_CHANGE_SKIN = "egame.launcher.widgetutil.appwidgetutils.service.action.ACTION_CHANGE_SKIN";
	public static final String ACTION_UTIL_WIDGET_WIFI_CLICKED = "egame.launcher.widgetutil.appwidgetutils.service.action.ACTION_UTIL_WIDGET_WIFI_CLICKED";
	public static final String ACTION_UTIL_WIDGET_BLUETOOTH_CLICKED = "egame.launcher.widgetutil.appwidgetutils.service.action.ACTION_UTIL_WIDGET_BLUETOOTH_CLICKED";
	public static final String ACTION_UTIL_WIDGET_GPS_CLICKED = "egame.launcher.widgetutil.appwidgetutils.service.action.ACTION_UTIL_WIDGET_GPS_CLICKED";
	public static final String ACTION_UTIL_WIDGET_DATA_CONNECTION_CLICKED = "egame.launcher.widgetutil.appwidgetutils.service.action.ACTION_UTIL_WIDGET_DATA_CONNECTION_CLICKED";
	public static final String ACTION_UTIL_WIDGET_RINGER_MODE_CLICKED = "egame.launcher.widgetutil.appwidgetutils.service.action.ACTION_UTIL_WIDGET_RINGER_MODE_CLICKED";
	public static final String ACTION_UTIL_WIDGET_BRIGHTNESS_CLICKED = "egame.launcher.widgetutil.appwidgetutils.service.action.ACTION_UTIL_WIDGET_BRIGHTNESS_CLICKED";
	public static final String ACTION_UTIL_WIDGET_FLASH_TORCH_CLICKED = "egame.launcher.widgetutil.appwidgetutils.service.action.ACTION_UTIL_WIDGET_FLASH_TORCH_CLICKED";

	private static final String TAG = WidgetUiService.class.getSimpleName();
	private BrightnessContentObserver mBrightnessContentObserver;

	@Override
	public void onCreate() {
		super.onCreate();

		mBrightnessContentObserver = new BrightnessContentObserver(
				new Handler(), this);
		ContentResolver contentResolver = getContentResolver();
		contentResolver.registerContentObserver(
				Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS),
				false, mBrightnessContentObserver);
		contentResolver.registerContentObserver(Settings.System
				.getUriFor(Settings.System.SCREEN_BRIGHTNESS_MODE), false,
				mBrightnessContentObserver);

	}

	@Override
	public void onDestroy() {
		ContentResolver contentResolver = getContentResolver();

		if (mBrightnessContentObserver != null) {
			contentResolver
					.unregisterContentObserver(mBrightnessContentObserver);
		} else {
			Log.e(TAG, "Error mBrightnessContentObserver == null");
		}

		super.onDestroy();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.d(TAG, "Called onStartCommand");
		// If received intent is invalid intent or intent with unknown action
		if (!Utils.isIntentOkay(intent)) {
			return stopUponBadIntent();
		}

		String action = intent.getAction();
		Log.d(TAG, "Action: " + action);

		if (ACTION_UTIL_WIDGET_WIFI_CLICKED.equals(action)) {
			boolean enable = WiFiUtility.isEnabled(this);
			WiFiUtility.setEnabled(this, !enable);
		} else if (ACTION_UTIL_WIDGET_DATA_CONNECTION_CLICKED.equals(action)) {
			boolean isEnabled = MobileDataUtility.isEnabled(this);
			MobileDataUtility.setEnabled(this, !isEnabled);
			Log.d(TAG, "data connection change to: " + !isEnabled);
		} else if (ACTION_UTIL_WIDGET_BRIGHTNESS_CLICKED.equals(action)) {
			int brightnessMode = BrightnessUtility.getBrightnessMode(this);
			brightnessMode = (brightnessMode + 1)
					% BrightnessUtility.NUMBER_OF_BRIGHTNESS_MODES;
			BrightnessUtility.setBrightnessMode(this, brightnessMode);
		} else if (ACTION_UTIL_WIDGET_RINGER_MODE_CLICKED.equals(action)) {

			int ringerMode = RingerModeUtitliy.getRingerMode(this);
			ringerMode = (ringerMode + 1)
					% RingerModeUtitliy.NUMBER_OF_RINGER_MODES;
			RingerModeUtitliy.setRingerMode(this, ringerMode);
		} else if (ACTION_UTIL_WIDGET_FLASH_TORCH_CLICKED.equals(action)) {
			int value = !FlashTorchUtility.isFlashLightEnabled(this) ? FlashTorchUtility.FLASH_TORCH_ENABLED
					: FlashTorchUtility.FLASH_TORCH_DISABLED;
			FlashTorchUtility.setFlashLightEnabled(this,
					!FlashTorchUtility.isFlashLightEnabled(this));
		}

		return Service.START_STICKY;
	}

	@Override
	public IBinder onBind(Intent intent) {

		return null;
	}

	private int stopUponBadIntent() {
		Log.d(TAG, "Started for a null or unknown Intent - stopping.");
		stopSelf();
		return Service.START_STICKY;
	}

	@Override
	public void brightnessStateChanged() {
		Log.d(TAG, "Called brightnessStateChanged");
		Intent intent = new Intent(
				BrightnessUtility.ACTION_CHANGE_BRIGHTNESS_STATE);
		sendBroadcast(intent);
	}
}
